<?php
class UserContentsController extends UsersAppController
{
	var $name = 'UserContents';
	var $components  = array('Email','Auth');
	var $uses = array('Users.BlockedItem','Users.ReportedItem');
	
	function block($type=null,$itemId=null)
	{
		if($this->data)
		{
			$params  = $this->_getBulkActionParams();
			$type = $params['model'];
			$itemId = $params['ids'];
		}
		$type = ucfirst($type);
		if($type && $itemId)
		{
			$this->BlockedItem->blockItems($type,$itemId,$this->Session->read('Auth.User.id'));
			$this->successMsg(sprintf(__('Selected %s have been blocked',true),$type));	
		}
		$this->redirect($this->referer());
		$this->render('blank');
	}
	
	function report($entity=null,$entityId=null)
	{
		if($entity && $entityId)
		{
			$entity = ucfirst($entity);
			$data['ReportedItem'] = array('entity_id'=>$entityId,'entity' => $entity ,'reporter_id'=>$this->Session->read('Auth.User.id'));
			$this->ReportedItem->save($data);
			$this->sendReportNotification($entity,$entityId);
			$this->successMsg(__('Thank you for reporting.',true));
		}
		$this->successMsg(__('Thank you for reporting.',true));
		$this->redirect($this->referer());
		$this->render('blank');
	}
	
	private function sendReportNotification($entity,$entityId)
	{
		$this->prepareEmail();
		$this->Email->to  = cr('cmsConfig.Backend.email');
		$this->Email->template = 'user_content_report';
		$this->Email->subject = __('New report for violated item.');
		$generationUrl = Router::url($this->referer(),true);
		$reporterId = $this->Session->read('Auth.User.id');
		$this->set(compact('entity','entityId','generationUrl','reporterId'));
		$this->Email->send();
	}
	
	function unblock($type=null,$itemId=null)
	{
		if($this->data)
		{
			$params  = $this->_getBulkActionParams();
			$type = $params['model'];
			$itemId = $params['ids'];
		}
		$type = ucfirst($type);
		if($type && $itemId)
		{
			$this->BlockedItem->removeBlockedItem($type,$itemId,$this->Session->read('Auth.User.id'));
			$this->successMsg(sprintf(__('Selected %s have been unblocked',true),$type));	
		}
		$this->redirect($this->referer());
		$this->render('blank');
	}

	function admin_setfeatured($modelAlias=null,$itemId=null)
	{
		if(empty($modelAlias) || empty($itemId))
		{
			$this->errorMsg(__('Invalid URL.'));
			$this->redirect($this->referer());

		}
		if(cri('Users.FeaturedItem')->setFeatured($modelAlias,$itemId))
		{
			$this->successMsg($modelAlias.' made featured');
			$this->redirect($this->referer());
		}
		else
		{
			$this->errorMsg($modelAlias.' could not be made featured. Please try again');
			$this->redirect($this->referer());
		}
		
	}

	function admin_removefeatured($modelAlias=null,$itemId=null)
	{
		if(empty($modelAlias) || empty($itemId))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect($this->referer());

		}
		if(cri('Users.FeaturedItem')->removeFeatured($modelAlias,$itemId))
		{
			$this->successMsg(sprintf(__('%s removed from featured List'),$modelAlias));
			$this->redirect($this->referer());
		}
		else
		{
			$this->errorMsg(sprintf(__('%s could not be made featured. Please try again'),$modelAlias));
			$this->redirect($this->referer());
		}
	}
	
}
?>